package com.chao.product.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSON;
import com.chao.model.PmsProduct;
import com.chao.product.conf.MyConfirmCallback;
import com.chao.product.conf.MyReturnCallback;
import com.chao.product.exception.handler.SystemBlockHandlerException;
import com.chao.product.service.ProductService;
import com.model.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Slf4j
@Controller
@RequestMapping(value = "product")
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    MyConfirmCallback myConfirmCallback;

    @Autowired
    MyReturnCallback myReturnCallback;

    private static boolean storeFlag = true;

    @GetMapping(value = "/getAllProduct")
    @ResponseBody
    @SentinelResource(value = "getAllProductController",
            blockHandler = "testHelloBlockHandler",
            blockHandlerClass = SystemBlockHandlerException.class)
    public CommonResult getAllProduct(Long s) {
        List<PmsProduct> pmsProducts = productService.getAllProduct();
        return CommonResult.success(pmsProducts);
    }


    @GetMapping(value = "/testSendMsg")
    @ResponseBody
    public void testSendMsg(){

        rabbitTemplate.setConfirmCallback(myConfirmCallback);

        rabbitTemplate.setReturnsCallback(myReturnCallback);

        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setId(1L);
//        pmsProduct.setDescription("helloWorld_queue");
//        rabbitTemplate.convertAndSend("helloWorld_queue",pmsProduct);
//
//        pmsProduct.setDescription("work_queue");
//        rabbitTemplate.convertAndSend("work_queue",pmsProduct);
//
//        pmsProduct.setDescription("direct_exchange-direct.queue.1");
//        rabbitTemplate.convertAndSend("direct_exchange","direct.queue.1",JSON.toJSONString(pmsProduct),new CorrelationData());
//        pmsProduct.setDescription("direct_exchange-direct.queue.2");
//        rabbitTemplate.convertAndSend("direct_exchange","direct.queue.2",JSON.toJSONString(pmsProduct),new CorrelationData());
//
//        pmsProduct.setDescription("fanout_exchange");
//        rabbitTemplate.convertAndSend("fanout_exchange","",JSON.toJSONString(pmsProduct),new CorrelationData());

        pmsProduct.setDescription("topic_exchange-topic.queue.1.xx");
        rabbitTemplate.convertAndSend("topic_exchange","topic.queue.1.xx",JSON.toJSONString(pmsProduct),new CorrelationData());
        pmsProduct.setDescription("topic_exchange-topic.queue.1.ss");
        rabbitTemplate.convertAndSend("topic_exchange","topic.queue.1.ss",JSON.toJSONString(pmsProduct),new CorrelationData());
        pmsProduct.setDescription("topic_exchange-topic.queue.2.aa.bb");
        rabbitTemplate.convertAndSend("topic_exchange","topic.queue.2.aa.bb",JSON.toJSONString(pmsProduct),new CorrelationData());
        pmsProduct.setDescription("topic_exchange-topic.queue.2.aa.bb.cc");
        rabbitTemplate.convertAndSend("topic_exchange","topic.queue.2.aa.bb.cc",JSON.toJSONString(pmsProduct),new CorrelationData());

    }

    @GetMapping(value = "/testRedis")
    @ResponseBody
    public void testRedis() {
//        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
//                .setNameFormat("lc-pool-%d").build();
//        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 200, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1000),namedThreadFactory);
//        for (int i = 1; i < 209; i++) {
//            threadPoolExecutor.execute(new TestRedis());
//        }
        if (storeFlag) {
            Long l = redisTemplate.opsForValue().decrement("lvchao1000");
            //log.info("剩余数量: {}",l);
            if (l < 0) {
                l = redisTemplate.opsForValue().increment("lvchao1000");
               // log.info("回滚: {}",l);
                storeFlag = false;
            }
        }
    }

}
